﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Web;
using System.Web.Mvc;
using WeiBo.Models;

namespace WeiBo.Controllers
{
    public class MusicController : Controller
    {
        WeiBoEntities db = new WeiBoEntities();
        // GET: Music
        /// <summary>
        /// 多表查询和数据展示
        /// </summary>
        /// <returns></returns>
        public ActionResult MusicIndex()
        {
            ViewBag.ReSou= db.ReSouTable.ToList();
            
            var userListTest = from g in db.GeRenInfo
                               join p in db.Personal on g.WBID equals p.WBID
                               select new
                               {
                                   PID = p.PID,
                                   name = g.GRName,
                                   jianjie = g.GRJianJie,
                                   img = g.GRImg,
                                   WenBen = p.PWenBen,
                                   TuPian = p.PTuPian,
                                   ShiPian = p.PShiPin,
                                   XiHuan = p.PXinHuan,
                                   ZhuanFa = p.PZhuanFa
                               };
            var key = Request["key"];
            if (key!=null)
            {
                 userListTest = from g in db.GeRenInfo
                                   join p in db.Personal on g.WBID equals p.WBID where g.GRName.Contains(key)
                                   select new
                                   {
                                       PID = p.PID,
                                       name = g.GRName,
                                       jianjie = g.GRJianJie,
                                       img = g.GRImg,
                                       WenBen = p.PWenBen,
                                       TuPian = p.PTuPian,
                                       ShiPian = p.PShiPin,
                                       XiHuan = p.PXinHuan,
                                       ZhuanFa = p.PZhuanFa
                                   };
            }
            List<dynamic> oneList = new List<dynamic>();
            foreach (var item in userListTest.ToList())
            {
                dynamic MusicList = new ExpandoObject();
                MusicList.PID = item.PID;
                MusicList.name = item.name;
                MusicList.jianjie = item.jianjie;
                MusicList.img = item.img;
                MusicList.WenBen = item.WenBen;
                MusicList.TuPian = item.TuPian;
                MusicList.ShiPian = item.ShiPian;
                MusicList.XiHuan = item.XiHuan;
                MusicList.ZhuanFa = item.ZhuanFa;
                oneList.Add(MusicList);
            }
            ViewBag.MusicList = oneList;
            return View();
        }

        /// <summary>
        /// 登录验证
        /// </summary>
        /// <param name="WBName"></param>
        /// <param name="WBPwd"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult MusicIndex(string WBName, int WBPwd)
        {
            var list = db.WeiBoInfo.Where(u => u.WBName == WBName && u.WBPwd == WBPwd).Count();
            if (list >0)
            {
                var user = db.WeiBoInfo.FirstOrDefault(u => u.WBName == WBName && u.WBPwd == WBPwd);
                Session["LUser"] = user;

                return RedirectToAction("SquareIndex", "Home", new { user.WBID });
            }
            return RedirectToAction("MusicIndex");
        }
        /// <summary>
        /// 评论显示
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult PLSelect(string id)
        {

            var userListTest1 = from c in db.CommentArea
                                join g in db.GeRenInfo on c.WBID equals g.WBID
                                where c.PID == id
                                select new
                                {
                                    name = g.GRName,
                                    plnr = c.Comments
                                };
            List<dynamic> oneList1 = new List<dynamic>();
            foreach (var item in userListTest1.ToList())
            {
                dynamic MusicList = new ExpandoObject();
                MusicList.name = item.name;
                MusicList.plnr = item.plnr;
                oneList1.Add(MusicList);
            }

            //先将对象转成字符串
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(oneList1);
            return Json(json);
        }

        /// <summary>
        /// 喜欢点击事件
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult XiHuan(string pid)
        {
           var list= db.Personal.FirstOrDefault(p => p.PID == pid);
            if (list.PXinHuan==null)
            {
                int a = 1;
                list.PXinHuan = a;
                db.SaveChanges();
            }
            else
            {
                int a = (int)list.PXinHuan;
                list.PXinHuan = a++;
                db.SaveChanges();
            }
            
            
            return View();
        }
    }
}